(function (o) {
	$p = null;	//Biến Processing
	START = 1;
	PLAYING = 2;	
	PAUSE = 3;
	STOP = 4;
	END = 5;
})(window);

var loop = 0;

var app = {
	frameRate : 6,
	width : 250,
	height : 250,
	ready : 3,
	last: null,
	status: PLAYING
};
	 
var rect = null;

/*
 * Start
 */
app.start = function(canvas_id)
{
	new Processing(canvas_id, app.init);
}

app.stop = function()
{
	$p.noLoop();
}

/*
 * Khởi tạo ban đầu
 */
app.init = function(p)
{
	$p = p;

	app.setup();
	$p.draw = app.draw;
	app.direction = $p.RIGHT;

	app.registEvents();
}

/*
 * Thiết lập Canvas
 */
app.setup = function()
{
	$p.size( app.width, app.height);
  	$p.strokeWeight( 1 );
  	$p.frameRate( app.frameRate );  	
  	$p.background( 100 );
  	$p.stroke(0,200,100);	
  	
  	rect = new Rect({x:0, y:0, width:10});
  	rect.drawInit();
  	
  	app.last = rect;
}

/*
 * Draw canvas
 */
app.draw = function()
{	
	//console.log("==================");
	//console.log("==================");
	
	if(app.ready != 0) 
	{
		app.ready--;
		console.log("Ready: " + app.ready);
	}
	else 
	{
		rect.move();
		
		if(app.status == PLAYING)
		{
			$p.background( 100 );
			rect.draw();
		}
	}
}

/*
 * Đăng ký các event
 */
app.registEvents = function()
{
	$p.keyPressed = function()	// Event nhấn keyboard
	{
		rect.direction = $p.keyCode; 	
		//console.log("Press key: " + $p.keyCode);	
	}
}